package com.baizhi.yyl.controller;

import com.baizhi.yyl.entity.Admin;
import com.baizhi.yyl.service.AdminService;
import com.baizhi.yyl.utils.ImageCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

@Controller
@RequestMapping("/admin")
@CrossOrigin
public class AdminController {
    @Autowired
    private AdminService adminService;

    @RequestMapping("logout")
    @ResponseBody
    public void logout(String token){
        System.out.println("logout: " + token);
        adminService.logout(token);
    }

    @RequestMapping("getImageCodes")
    @ResponseBody
    public Map<String,String> getImageCodes(HttpSession session) {
        return adminService.getImageCodes(session.getId());
    }

    @RequestMapping("login")
    @ResponseBody
    public Map<String,String> login(@RequestBody Admin admin, String token){
        return adminService.login(admin, token);
    }

    @RequestMapping("getUserInfo")
    @ResponseBody
    public Admin getUserInfo(String token){
        return adminService.getUserInfo(token);
    }
}
